package com.qf.shiro;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.RememberMeManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
import java.util.LinkedHashMap;

@Configuration
public class ShiroConfig  {

    @Bean
    public ShiroDialect shiroDialect() {
        return new ShiroDialect();
    }

    @Bean("myRealm")
    public MyRealm myRealm(){
        MyRealm myRealm=new MyRealm();
        HashedCredentialsMatcher hashedCredentialsMatcher=new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashIterations(1024);
        hashedCredentialsMatcher.setHashAlgorithmName("Md5");
        myRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        return myRealm;
    }

    @Bean("rememberme")
    public SimpleCookie simpleCookie(){
        SimpleCookie simpleCookie=new SimpleCookie();
        simpleCookie.setMaxAge(60*60*24);
        simpleCookie.setHttpOnly(true);
        simpleCookie.setName("RememberMe1905");
        return simpleCookie;
    }

    @Bean("remembermeManager")
    public CookieRememberMeManager cookieRememberMeManager(@Qualifier("rememberme") SimpleCookie simpleCookie) throws NoSuchAlgorithmException {
        CookieRememberMeManager cookieRememberMeManager=new CookieRememberMeManager();
        cookieRememberMeManager.setCookie(simpleCookie);
        KeyGenerator keygen = KeyGenerator.getInstance("AES");
        SecretKey deskey = keygen.generateKey();
        cookieRememberMeManager.setCipherKey(deskey.getEncoded());
        System.out.println("===========");
        System.out.println(cookieRememberMeManager.getCipherKey());
        System.out.println("===========");
        return cookieRememberMeManager;
    }

    @Bean("manager")
    public SecurityManager securityManager(@Qualifier("myRealm")MyRealm myRealm,@Qualifier("remembermeManager") RememberMeManager rememberMeManager){
        DefaultWebSecurityManager manager=new DefaultWebSecurityManager();
        manager.setRealm(myRealm);
        manager.setRememberMeManager(rememberMeManager);
        return manager;
    }

    @Bean
    public ShiroFilterFactoryBean factoryBean(@Qualifier("manager")SecurityManager manager){
        ShiroFilterFactoryBean factoryBean=new ShiroFilterFactoryBean();
        factoryBean.setSecurityManager(manager);
        factoryBean.setLoginUrl("/user/login");
        factoryBean.setSuccessUrl("/user/index");

        LinkedHashMap<String,String> hashMap=new LinkedHashMap<>();
        hashMap.put("/user/login","anon" );
        hashMap.put("/user/logout","anon" );
        hashMap.put("/user/index","anon" );
        hashMap.put("/user/tologin","anon" );
        hashMap.put("/user/test","anon" );
        hashMap.put("/user/*","authc" );
        hashMap.put("/shopping/*", "authc");

        hashMap.put("/user/logout","logout");
        hashMap.put("/static/**","anon");//静态js  css

        factoryBean.setFilterChainDefinitionMap(hashMap);
        return factoryBean;



    }





}
